package com.itheima.case2.service;

import com.itheima.case2.dao.UserMapper1;
import com.itheima.case2.pojo.po.User;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl_1 {
    public User login(User u) {
        //获取mybatis会话对象
        SqlSession sqlSession = SqlSessionUtil.getSession(false);
        //使用会话对象调用方法获取接口代理对象
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        //使用接口代理对象调用接口登陆方法，返回User对象
        User user = mapper.login(u);
        //释放资源
        sqlSession.close();
        //将查询的User对象返回给web层
        return user;
    }

    public boolean register(User user) {
        SqlSession sqlSession=null;
        try {
            sqlSession = SqlSessionUtil.getSession(true);
            UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
            String username = user.getUsername();
            User u = mapper.queryUserByUsername(username);
            if(u==null){
                mapper.register(user);
                return true;
            }else{
                return false;
            }
        } finally {
            sqlSession.close();
        }
    }
}
